from scenedetect import VideoManager
from scenedetect import SceneManager
from scenedetect.detectors import ContentDetector

from pymongo import MongoClient
threshold=30.0
client = MongoClient(port=27017)
db=client['tiktok']
collist=['cancelthenoise', 'itwasntme', 'beatsdaisychallenge', 'monclerbubbleup', 'airpodsjump', 'inmyaejeans', 'thisisbliss', 'gotmilkchallenge', 'expressieyourself', 'dopacsun', 'strictlycurls', 'hereforrmhc', 'boseallout', 'moodflip', 'whatsyourpower', 'asosfashunweek', 'letsfaceit', 'showmeyourwalk', 'scoobdance', 'merrybossmas']

for col in collist:
    for obj in db[col].find():
        if 'avg_scences' not in obj['video_feature']['editing'].keys():
            if 'video_len' not in obj['video_feature']['editing'].keys():
                continue
            video_manager = VideoManager(['E:\\Tiktok\\Hashtag\\'+col+'\\' + obj['_id'] + '.mp4'])
            scene_manager = SceneManager()
            scene_manager.add_detector(ContentDetector(threshold=threshold))
            base_timecode = video_manager.get_base_timecode()
            video_manager.set_downscale_factor()
            video_manager.start()
            scene_manager.detect_scenes(frame_source=video_manager)
            scenes = scene_manager.get_scene_list(base_timecode)

            db[col].update_one({'_id':obj['_id']},{'$set':{'video_feature.editing.avg_scences':obj['video_feature']['editing']['video_len']/len(scenes)}})
            print('updated',obj['_id'])